package com.foreknow.demo18;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 我们要将查询到的数据以对象的方式保存到集合里面
 */
public class JDBCDemo {
    public static void main(String[] args) {
        //1.加载驱动程序
        //如果是8.0以上的版本需要使用com.mysql.cj.jdbc.Driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
            //获取statement对象
            Statement stmt =conn.createStatement();

           ResultSet rs = stmt.executeQuery("select *from emp");
            List<Emp> list = new ArrayList<>();
           while (rs.next()){
               Emp emp = new Emp();

              int empno =rs.getInt("empno");
              String ename=rs.getNString("ename");
              String job = rs.getString("job");
              //将获取到得当前行数据 保存到Emp对象里
              emp.setEmpno(empno);
              emp.setEname(ename);
              emp.setJob(job);
              //将当前对象保存到list集合里面
              list.add(emp);

//              Date hir = rs.getDate("hiredate");
//              System.out.println(empno+"   "+ename+"   "+"   "+job);
           }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
